package com.ruoyi.project.online_experimentStu.service.impl;

import com.ruoyi.common.exception.BusinessException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.project.online_experimentStu.domain.LookExperiment;
import com.ruoyi.project.online_experimentStu.domain.OnlineExperimentStu;
import com.ruoyi.project.online_experimentStu.domain.OnlineExperimentTea;
import com.ruoyi.project.online_experimentStu.domain.Student;
import com.ruoyi.project.online_experimentStu.mapper.OnlineExperimentStuMapper;
import com.ruoyi.project.online_experimentStu.service.OnlineExperimentStuService;
import com.ruoyi.project.system.dept.domain.Dept;
import com.ruoyi.project.system.user.domain.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;
import java.util.UUID;

/**
 * @ClassName OnlineExperimentStuServiceImpl
 * @Description TODO
 * @Author lvyongwei
 * @Date 2020-04-16 16:23
 **/
@Service
public class OnlineExperimentStuServiceImpl implements OnlineExperimentStuService {

    @Autowired
    private OnlineExperimentStuMapper onlineExperimentStuMapper;
    /**
     * 生成新实验，分配学生做实验
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String experimentalSetup(OnlineExperimentTea onlineExperimentTea, List<Student> students,boolean updateSupport) {
        //System.err.println(onlineExperimentTea);
        //设置分配的实验的id
        String experimentTeaId=UUID.randomUUID().toString();
        onlineExperimentTea.setExperimentTeaId(experimentTeaId);
        //分配实验
        onlineExperimentStuMapper.experimentTest(onlineExperimentTea);
        //添加实验对应学生
        if(onlineExperimentTea.getDeptId()!=null){
            //班级不为空,则说明按照班级导入学生
            //获取该班级的所有学生
            List<Student> lists=onlineExperimentStuMapper.getAllStudentByDeptId(onlineExperimentTea.getDeptId());
            int count=0;
            for(Student stu:lists){
                stu.setExperimentTeaId(experimentTeaId);
                //onlineExperimentStuMapper.correspondingPerson(stu);
                boolean flag=onlineExperimentStuMapper.correspondingPerson(stu);
                if(flag==true){
                    count++;
                }
            }
            if(count>0){
                return "导入成功";
            }else{
                return "导入失败,请检查该部门是否没有学生";
            }
        }
        else if(onlineExperimentTea.getProfession()!=null){
            //获取该专业下的所有身份位学生的学生
            List<Student> stus=onlineExperimentStuMapper.getAllStudentByProfession(onlineExperimentTea.getProfession());
            //System.err.println(stus);
            int count=0;
            for(Student stu:stus){
                stu.setExperimentTeaId(experimentTeaId);
                boolean flag=onlineExperimentStuMapper.correspondingPerson(stu);
                if(flag==true){
                    count++;
                }
            }
            if(count>0){
                return "导入成功";
            }else{
                return "导入失败，请检查该专业是否没有学生";
            }
        }
        else if(students.size()!=0 || students==null){
            //通过文件导入学生
            int successNum = 0;
            int failureNum = 0;
            StringBuilder successMsg = new StringBuilder();
            StringBuilder failureMsg = new StringBuilder();
            for (Student stu : students)
            {
                try
                {
                    stu.setExperimentTeaId(experimentTeaId);
                    onlineExperimentStuMapper.correspondingPerson(stu);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、账号 " + stu.getStudentName() + " 导入成功");
                }
                catch (Exception e)
                {
                    failureNum++;
                    String msg = "<br/>" + failureNum + "、账号 " + stu.getStudentName() + " 导入失败：";
                    failureMsg.append(msg + e.getMessage());
                }
            }
            if (failureNum > 0)
            {
                failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                throw new BusinessException(failureMsg.toString());
            }
            else
            {
                successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
            }
            return successMsg.toString();
        }else{
            return "导入学生失败";
        }
    }

    /**
     * 获取学生班级,即部门
     * @return
     */
    @Override
    public List<Dept> getStudentDept() {
        return onlineExperimentStuMapper.getStudentDept();
    }


    /**
     * 获取所有布置的实验
     * @param onlineExperimentTea
     * @return
     */
    @Override
    public List<OnlineExperimentTea> getAllOnlineExperimentTea(OnlineExperimentTea onlineExperimentTea) {
        return onlineExperimentStuMapper.getAllOnlineExperimentTea(onlineExperimentTea);
    }


    /**
     * 获取到所有学生试验情况
     * @param onlineExperimentStu
     * @return
     */
    @Override
    public List<OnlineExperimentStu> getAllOnlineExperimentStu(OnlineExperimentStu onlineExperimentStu) {
        return onlineExperimentStuMapper.getAllOnlineExperimentStu(onlineExperimentStu);
    }


    /**
     * 学生查看实验
     * @param lookExperiment
     * @return
     */
    @Override
    public List<LookExperiment> getAllLookExperiment(LookExperiment lookExperiment) {
        return onlineExperimentStuMapper.getAllLookExperiment(lookExperiment);
    }

    /**
     * 学生点击开始试验
     * @param experimentTeaId
     * @return
     */
    @Override
    public LookExperiment getLookExperiment(String experimentTeaId) {
        return onlineExperimentStuMapper.getLookExperiment(experimentTeaId);
    }

    /**
     * 学生点击重做试验
     * @param experimentTeaId
     * @return
     */
    @Override
    public LookExperiment getLookExperiment1(String experimentTeaId,Long studentId) {
        return onlineExperimentStuMapper.getLookExperiment1(experimentTeaId,studentId);
    }

    /**
     * 学生提交实验成绩
     */
    @Override
    public boolean updateExperimentStu(OnlineExperimentStu onlineExperimentStu) {
        return onlineExperimentStuMapper.updateExperimentStu(onlineExperimentStu);
    }

    /**
     * 查看教师评分等信息
     * @param map
     * @return
     */
    @Override
    public OnlineExperimentStu getOnlineExperimentStu(Map<String,Object> map){
        return onlineExperimentStuMapper.getOnlineExperimentStu(map);
    }

    /**
     * 根据布置的实验id取货去布置的所有内容
     * @param experimentTeaId
     * @return
     */
    @Override
    public OnlineExperimentTea getOnlineExperimentTea(String experimentTeaId) {
        return onlineExperimentStuMapper.getOnlineExperimentTea(experimentTeaId);
    }

    /**
     * 删除教师布置的实验即学生提交的实验的相关内容
     * @param experiemntTeaId
     * @return
     */
    @Override
    @Transactional
    public boolean deleteExperimentTea(String experiemntTeaId) {
        onlineExperimentStuMapper.deleteOnlineExperimentStu(experiemntTeaId);
        return onlineExperimentStuMapper.deleteOnlineExperimentTea(experiemntTeaId);
    }

    /**
     * 根据布置的实验id获取到布置的实验
     * @param experimentTeaId
     * @return
     */
    @Override
    public OnlineExperimentTea getExperimentTeaById(String experimentTeaId) {
        return onlineExperimentStuMapper.getExperimentTeaById(experimentTeaId);
    }


    /**
     * 修改布置的实验结束时间
     * @param onlineExperimentTea
     * @return
     */
    @Override
    public boolean editExperimentTeaTime(OnlineExperimentTea onlineExperimentTea) {
        return onlineExperimentStuMapper.editExperimentTeaTime(onlineExperimentTea);
    }

    /**
     * 获取教师布置的实验的学生
     * 的成绩
     * @param experimentTeaId
     * @return
     */
    @Override
    public List<OnlineExperimentStu> getResult(String experimentTeaId) {
        return onlineExperimentStuMapper.getResult(experimentTeaId);
    }


    /**
     * 获取项目提供的实验报告模板
     * @param experimentTeaId
     * @return
     */
    @Override
    public String getExperimentReport(String experimentTeaId) {
        return onlineExperimentStuMapper.getExperimentReport(experimentTeaId);
    }

}
